XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	// This variable will hold all the groups of the reinforcement vehicles.
	DM_Mission_CounterAttackGroups = [];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};
	_town_Lopatino = d_town_markers select 25;
	_town_Lopatino_pos = _town_Lopatino select 0;
	_town_Lopatino_size = _town_Lopatino select 2;

	_conquer_updated = false;
	_counterattack_created = false;
	_counterattack_updated = false;
	_start_enemy_counts = ([_town_Lopatino_pos,_town_Lopatino_size,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
	_counter_attack_trigger = round(_start_enemy_counts * 0.15);
	diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Lopatino Start",_start_enemy_counts,_counter_attack_trigger];
	sleep 10;
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_counts = [_town_Lopatino_pos,_town_Lopatino_size,d_enemy_side] call XfMissionGetZoneSideCount;
		_lopatino_freed = ["jip_lopatino_freed",false] call XNetGetJIP;
		_counter_attack_alive= false;
		{
			if (alive (leader _x)) exitWith {_counter_attack_alive = true;};
		} foreach DM_Mission_CounterAttackGroups;
		// diag_log Format["(%1)[%2]: %3: %4, %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute",_enemy_counts,_lopatino_freed,_counter_attack_alive];
		if (((_enemy_counts select 0) < _counter_attack_trigger) AND (!_counterattack_created)) then {
			//Since we used the Reinforcement 1 option in the UPSMON arrays, we need to set the values accordingly
			KRON_UPS_reinforcement1 = True;
			KRON_UPS_reinforcement1_pos = [2753.65,9926.02,0];
			If (!_conquer_updated) then {
				["FreeLopatino","", "","",[],"Succeeded"] call XfMissionTask;
				_conquer_updated = true;
			};
			["DefendLopatino","Defend Lopatino", "You need to defend Lopatino from the counter attackers!","",[],"Created"] call XfMissionTask;
			_counterattack_created = true;
			"Watch out! A counter attack force is on the way!" call XfMissionMessageToAll;
		};
		if (_lopatino_freed and !_conquer_updated) then {
			["FreeLopatino","Free Lopatino", "","",[],"Succeeded"] call XfMissionTask;
			_conquer_updated = true;
		};
		If ((!_counter_attack_alive and ((_enemy_counts select 0) <= 3)) AND !_counterattack_updated) then {
			["DefendLopatino","Defend Lopatino", "","",[],"Succeeded"] call XfMissionTask;
			_counterattack_updated = true;
			_chapter_succeeded = true;
			DM_Mission_Chapter_CanRun = false;
			"The counter attack force has been dismissed!" call XfMissionMessageToAll;
		};
		If (DM_Mission_Chapter_CanRun) then {
			// Sending random reinforcement if configured in the mission editor and if there are enough remaining troops.
			If (player_is_detected and (DM_Mission_RandomReinforcement > 0) and ((_enemy_counts select 0) > _counter_attack_trigger)) then {
				_random_reinf_diff = time - _last_random_reinforcement;
				// Adding some randomness to the amount
				_random_reinf_diff = floor(_random_reinf_diff - (_random_reinf_diff / 3) + floor(random(_random_reinf_diff / 2)));
				// The following test makes that at the max value (6) a reinforcement will be sent around every 10 minutes.
				// While at the lowest values, it will be every hour.
				if ((_random_reinf_diff * DM_Mission_RandomReinforcement) >= 3600) then {
					_reinforcement_power = DM_Mission_RandomReinforcement / 2;
					[[DM_Chapter_Position,100],"PARA",_reinforcement_power] call XfMissionCallReinforcement;
					diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Random reinforcement sent!"];
					hint "Random Reinforcement";
					_last_random_reinforcement = time;
				};
			};
		};
	};
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Mission canceled!"];
	};
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_town_Lopatino","_town_Lopatino_pos","_town_Lopatino_size","_troops_power","_troops_coeff","_grpSpecOps1","_grpSpecOps2"];
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization"];
	_town_Lopatino = d_town_markers select 25;
	_town_Lopatino_pos = _town_Lopatino select 0;
	_town_Lopatino_size = _town_Lopatino select 2;
	// This indicates where the mission takes place
	[_town_Lopatino_pos,_town_Lopatino_size,_town_Lopatino select 1] call XfMissionCurrentPosition;
	// We remove the start teleporter in case it was not cleaned properly in another mission.
	[[3276.42,11386.4,0.1],156] call XfMissionPlaceStartTeleporter;
	[false,["chapter1_start_misson", [3276.42,11386.4,0.1], "ICON", "ColorBlue", [0.7,0.7],"Start",156,"hd_start","",1]] call XfMissionMarker;

	// The marker which indicates the war zone	
	[false,["chapter1_town_position", _town_Lopatino_pos, "ELLIPSE", "ColorRed", [(_town_Lopatino_size * 1.5),(_town_Lopatino_size * 1.5)],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	// The marker which is used by reinforcement	
	[true,["upsmon_reinforcement", _town_Lopatino_pos, "ELLIPSE", "ColorRed", [_town_Lopatino_size,_town_Lopatino_size],"",0,"","BDiagonal",0.001]] call XfMissionMarker;

	[d_enemy_side,[_town_Lopatino_pos,_town_Lopatino_size],true] call XfMissionPopulateLocation;
	// We create two groups which will wait and ambush in town.
	_grpSpecOps1 = [d_enemy_side,[[2711.74,10026.6,0],50],[],1,false] call XfMissionCreateFootmenGroup;
	_grpSpecOps2 = [d_enemy_side,[[2753.65,9926.02,0],50],[],1,false] call XfMissionCreateFootmenGroup;
	
	// We now create the "object" which is used to create the zone to conquer. It serves as a variable holder more than anything.
	["jip_lopatino_freed",false] call XNetSetJIP;
	["HeliHEmpty",_town_Lopatino_pos,"CONQUER",["The town of Lopatino is now clear!",[_town_Lopatino_pos, [(_town_Lopatino_size * 1.5),(_town_Lopatino_size * 1.5)], 0, false],"jip_lopatino_freed"]] call XfMissionCreateMissionObject;
	["FreeLopatino","Free Lopatino", "You need to drive the enemy forces out of Lopatino!","",[],"Created"] call XfMissionTask;

	// Now we'll create the counter attack units. They will spawn either on the road to Grozovoy Pass or in Kabanino
	// Grozovoy Pass
	_spawn_position = [1325.32,13451,0];
	// Kabanino
	if (floor(random(10)) >= 5) then {_spawn_position = [5328.43,8568.63,0];};

	_players = call XPlayersNumber;
	_troops_counter_attack = 1;
	switch (true) do {
		case (_players <= 5): { _troops_counter_attack = 1 };
		case (_players <= 10): { _troops_counter_attack = 1 };
		case (_players <= 15): { _troops_counter_attack = 2 };
		case (_players <= 20): { _troops_counter_attack = 2 };
		default {_troops_counter_attack = 3;};
	};
	
	KRON_UPS_reinforcement1_pos = [0,0];
	KRON_UPS_reinforcement1 = False;
	
	// We create a bit of all units
	If (_troops_counter_attack > 1) then {
		_grpTank = [d_enemy_side,[_spawn_position,50],["upsmon_reinforcement","onroad","safe","fortify","reinforcement:",1],0] call XfMissionCreateVehicleGroup;
		DM_Mission_CounterAttackGroups = DM_Mission_CounterAttackGroups + [_grpTank];
	};
	// For all other units than tank, we create from 1 to multiple units depending on how many players are connected.
	for "_i" from 1 to _troops_counter_attack do {
		// 50% chance to skip it.
		if (floor(random(10)) >= 5) then {
			_grpBmp = [d_enemy_side,[_spawn_position,50],["upsmon_reinforcement","onroad","safe","fortify","reinforcement:",1],1] call XfMissionCreateVehicleGroup;
			DM_Mission_CounterAttackGroups = DM_Mission_CounterAttackGroups + [_grpBmp];
		};
	};
	for "_i" from 1 to _troops_counter_attack do {
		if (floor(random(10)) >= 5) then {
			_grpBrdm = [d_enemy_side,[_spawn_position,50],["upsmon_reinforcement","onroad","safe","fortify","reinforcement:",1],2] call XfMissionCreateVehicleGroup;
			DM_Mission_CounterAttackGroups = DM_Mission_CounterAttackGroups + [_grpBrdm];
		};
	};
	for "_i" from 1 to _troops_counter_attack do {
		_grpVodnik = [d_enemy_side,[_spawn_position,50],["upsmon_reinforcement","onroad","safe","fortify","reinforcement:",1],4] call XfMissionCreateVehicleGroup;
		DM_Mission_CounterAttackGroups = DM_Mission_CounterAttackGroups + [_grpVodnik];
	};
	// We increase the number of UAZ, because they are easy to deal with when defending.
	for "_i" from 1 to (_troops_counter_attack * 2) do {
		_grpUaz = [d_enemy_side,[_spawn_position,50],["upsmon_reinforcement","onroad","safe","fortify","reinforcement:",1],5] call XfMissionCreateVehicleGroup;
		DM_Mission_CounterAttackGroups = DM_Mission_CounterAttackGroups + [_grpUaz];
	};
	
	// The trigger which will send reinforcement when the player is detected!
	player_is_detected = false;
	[_town_Lopatino_pos,_town_Lopatino_size,d_own_side,d_enemy_side,"player_is_detected"] call XfMissionSideDetected;
	
	// This will create snipers automatically if selected in the mission setup
	[d_enemy_side,[_town_Lopatino_pos,_town_Lopatino_size],DM_Mission_Snipers] call XfMissionCreateSnipersArea;
	// This will create air support automatically if selected in the mission setup
	[d_enemy_side,[_town_Lopatino_pos,_town_Lopatino_size],DM_Mission_AirSupport] call XfMissionCallAirSupport;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanObjects;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;